<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="什么是迭代器迭代器 是指确使用户可在容器对象（container，例如链表或数组）上遍访的对象，使用该接口无需关心对象的内部实现细节。 在很多编程语言里，都有自己的迭代器。JavaScript也不例外。在JavaScript中，迭代器是一个对象，它需要符合一种协议，这种协议叫做 迭代协议。 这个对象中需要有一个叫做 next 的函数，这个函数 必须有以下要求 ：  它的参数是一个或者没有。 它的返">
<meta property="og:type" content="article">
<meta property="og:title" content="迭代器与生成器">
<meta property="og:url" content="https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/index.html">
<meta property="og:site_name" content="柯里的语法糖">
<meta property="og:description" content="什么是迭代器迭代器 是指确使用户可在容器对象（container，例如链表或数组）上遍访的对象，使用该接口无需关心对象的内部实现细节。 在很多编程语言里，都有自己的迭代器。JavaScript也不例外。在JavaScript中，迭代器是一个对象，它需要符合一种协议，这种协议叫做 迭代协议。 这个对象中需要有一个叫做 next 的函数，这个函数 必须有以下要求 ：  它的参数是一个或者没有。 它的返">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-05T12:31:10.768Z">
<meta property="article:modified_time" content="2022-03-05T12:31:10.768Z">
<meta property="article:author" content="柯里的语法糖">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
    
    
      
        
          <link rel="shortcut icon" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
    
    <!-- title -->
    <title>迭代器与生成器</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.0.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="上一篇" href="/2022/03/05/network/http/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="下一篇" href="/2022/03/05/frontend/Vue_Mastery%E7%AC%94%E8%AE%B0/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="返回顶部" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="分享文章" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&text=迭代器与生成器"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&is_video=false&description=迭代器与生成器"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=迭代器与生成器&body=Check out this article: https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&name=迭代器与生成器&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&t=迭代器与生成器"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">1.</span> <span class="toc-text">什么是迭代器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">2.</span> <span class="toc-text">编写一个迭代器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.</span> <span class="toc-text">可迭代对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E7%9A%84%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.</span> <span class="toc-text">内置的可迭代对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">5.</span> <span class="toc-text">可迭代对象的应用场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E7%9A%84%E8%BF%AD%E4%BB%A3"><span class="toc-number">6.</span> <span class="toc-text">自定义类的迭代</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%AD%E4%BB%A3%E5%99%A8%E7%9A%84%E4%B8%AD%E6%96%AD"><span class="toc-number">7.</span> <span class="toc-text">迭代器的中断</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E7%94%9F%E6%88%90%E5%99%A8"><span class="toc-number">8.</span> <span class="toc-text">什么是生成器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E5%87%BD%E6%95%B0%E7%9A%84%E6%89%A7%E8%A1%8C%E8%BF%87%E7%A8%8B%E4%B8%8E%E4%BC%A0%E5%8F%82"><span class="toc-number">9.</span> <span class="toc-text">生成器函数的执行过程与传参</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E6%8F%90%E5%89%8D%E7%BB%93%E6%9D%9F"><span class="toc-number">10.</span> <span class="toc-text">生成器提前结束</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8"><span class="toc-number">11.</span> <span class="toc-text">生成器抛出异常</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%94%9F%E6%88%90%E5%99%A8%E4%BB%A3%E6%9B%BF%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">12.</span> <span class="toc-text">使用生成器代替迭代器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%94%9F%E6%88%90%E5%99%A8%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E7%9A%84%E8%BF%AD%E4%BB%A3"><span class="toc-number">13.</span> <span class="toc-text">使用生成器实现自定义类的迭代</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        迭代器与生成器
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">柯里的语法糖</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2022-03-05T12:31:10.768Z" itemprop="datePublished">2022-03-05</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/">前端基础</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link-link" href="/tags/JavaScript/" rel="tag">JavaScript</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h2 id="什么是迭代器"><a href="#什么是迭代器" class="headerlink" title="什么是迭代器"></a>什么是迭代器</h2><p><strong>迭代器</strong> 是指确使用户可在容器对象（container，例如链表或数组）上遍访的对象，使用该接口无需关心对象的内部实现细节。</p>
<p>在很多编程语言里，都有自己的迭代器。JavaScript也不例外。在JavaScript中，迭代器是一个对象，它需要符合一种协议，这种协议叫做 <strong>迭代协议</strong>。</p>
<p>这个对象中需要有一个叫做 <code>next</code> 的函数，这个函数 <strong>必须有以下要求</strong> ：</p>
<ol>
<li>它的参数是一个或者没有。</li>
<li>它的返回值是一个包含 <code>done</code> 属性 和 <code>value</code> 属性的对象。</li>
<li><code>done</code> 属性是一个布尔值，如果迭代器可以产生序列中的下一个值，则为 <code>false</code> 。如果迭代器已将序列迭代完毕，则为 <code>true</code> 。这种情况下，<code>value</code> 是可选的，如果它依然存在，即为迭代结束之后的默认返回值。</li>
<li><code>value</code> 属性表示迭代器返回的值。</li>
</ol>
<h2 id="编写一个迭代器"><a href="#编写一个迭代器" class="headerlink" title="编写一个迭代器"></a>编写一个迭代器</h2><p>下面这个例子，我们来编写一个自己的迭代器。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> names = [<span class="string">&#x27;curry&#x27;</span>, <span class="string">&#x27;james&#x27;</span>, <span class="string">&#x27;harden&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createNamesIterator</span>(<span class="params">names</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">next</span>:<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (index &lt; names.length) <span class="keyword">return</span> &#123; <span class="attr">done</span>: <span class="literal">false</span>, <span class="attr">value</span>: names[index++] &#125;</span><br><span class="line">      <span class="keyword">else</span> <span class="keyword">return</span> &#123; <span class="attr">done</span>: <span class="literal">true</span>, <span class="attr">value</span>: <span class="literal">undefined</span> &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> namesIterator = createNamesIterator(names)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br></pre></td></tr></table></figure>

<h2 id="可迭代对象"><a href="#可迭代对象" class="headerlink" title="可迭代对象"></a>可迭代对象</h2><p><strong>可迭代对象和迭代器是不同的概念</strong>：</p>
<ul>
<li>迭代器是一个对象，它实现的是迭代器协议(iterator protocol)，要实现一个next方法</li>
<li>可迭代对象是一个对象，它实现的是可迭代协议(iterable protocol)，要实现一个 [Symbol.iterator] 的方法，该方法返回一个迭代器</li>
</ul>
<p>当一个对象实现了可迭代协议的时候，那么它就是一个可迭代对象。</p>
<p>可迭代对象要求必须实现 <code>@@iterator</code> 方法，这意味着对象（或者它原型链上的某个对象）必须有一个键为 <code>@@iterator</code> 的属性，可通过常量 <code>Symbol.iterator</code> 访问该属性。</p>
<blockquote>
<p>当一个对象变成可迭代对象的时候，可以进行某些迭代操作。比如 <code>for of</code> 操作，就是调用 <code>@@iterator</code> 方法。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> iterableObj = &#123;</span><br><span class="line">  <span class="attr">names</span>: [<span class="string">&#x27;curry&#x27;</span>, <span class="string">&#x27;james&#x27;</span>, <span class="string">&#x27;harden&#x27;</span>],</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">next</span>: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (index &lt; <span class="built_in">this</span>.names.length)</span><br><span class="line">          <span class="keyword">return</span> &#123; <span class="attr">done</span>: <span class="literal">false</span>, <span class="attr">value</span>: <span class="built_in">this</span>.names[index++] &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">return</span> &#123; <span class="attr">done</span>: <span class="literal">true</span>, <span class="attr">value</span>: <span class="literal">undefined</span> &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">of</span> iterableObj) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(item)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="内置的可迭代对象"><a href="#内置的可迭代对象" class="headerlink" title="内置的可迭代对象"></a>内置的可迭代对象</h2><p>JavaScript的很多内置对象都实现了可迭代协议，会生成一个迭代器对象：</p>
<p>包括： <code>String</code> 、<code>Array</code> 、<code>Map</code> 、<code>Set</code> 、<code>arguments对象</code> 、<code>NodeList集合</code> 。</p>
<h2 id="可迭代对象的应用场景"><a href="#可迭代对象的应用场景" class="headerlink" title="可迭代对象的应用场景"></a>可迭代对象的应用场景</h2><p>可迭代对象在很多地方都被使用：</p>
<ol>
<li><code>for of</code></li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 上一个例子</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">of</span> iterableObj) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(item)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>展开语法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="keyword">const</span> arr2 = [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line"><span class="keyword">const</span> arr3 = [...arr1, ...arr2]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：对象的展开运算符并不是使用的迭代器，而是ES9新增的特性。</p>
</blockquote>
<ol start="3">
<li>解构语法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> [name1, name2] = names</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：对象的解构并不是使用的迭代器，而是ES9新增的特性。</p>
</blockquote>
<ol start="4">
<li>创建其他对象</li>
</ol>
<p>例如 <code>Map</code> 、 <code>Set</code> 、<code>Promise.all</code> 中，我们传入的参数必须是一个可迭代对象。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> set1 = <span class="keyword">new</span> <span class="built_in">Set</span>(iterableObj)</span><br><span class="line"><span class="keyword">const</span> set2 = <span class="keyword">new</span> <span class="built_in">Set</span>(names)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr1 = <span class="built_in">Array</span>.from(iterableObj)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 5.Promise.all</span></span><br><span class="line"><span class="built_in">Promise</span>.all(iterableObj).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(res)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="自定义类的迭代"><a href="#自定义类的迭代" class="headerlink" title="自定义类的迭代"></a>自定义类的迭代</h2><p>如果我们要设计一个类，并且默认这个类创建处理的是可迭代对象，那么我们在原型上需要加上 <code>@@iterator</code> 方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Team</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name, players</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name</span><br><span class="line">    <span class="built_in">this</span>.players = players</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]() &#123;</span><br><span class="line">    <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">next</span>: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (index &lt; <span class="built_in">this</span>.players.length) &#123;</span><br><span class="line">          <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">done</span>: <span class="literal">false</span>,</span><br><span class="line">            <span class="attr">value</span>: <span class="built_in">this</span>.players[index++],</span><br><span class="line">          &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">done</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="attr">value</span>: <span class="literal">undefined</span>,</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> team1 = <span class="keyword">new</span> Team(<span class="string">&#x27;gsw&#x27;</span>, [<span class="string">&#x27;curry&#x27;</span>, <span class="string">&#x27;green&#x27;</span>, <span class="string">&#x27;klay&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> team2 = <span class="keyword">new</span> Team(<span class="string">&#x27;lakers&#x27;</span>, [<span class="string">&#x27;ad&#x27;</span>, <span class="string">&#x27;james&#x27;</span>, <span class="string">&#x27;kobe&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> player <span class="keyword">of</span> team1) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(player)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> player <span class="keyword">of</span> team2) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(player)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="迭代器的中断"><a href="#迭代器的中断" class="headerlink" title="迭代器的中断"></a>迭代器的中断</h2><p>在某些情况下，迭代器会在没有完全迭代完的时候被中断掉。</p>
<p>如果我们需要监听中断，可以实现 <code>return</code> 方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> iterableObj = &#123;</span><br><span class="line">  <span class="attr">names</span>: [<span class="string">&#x27;curry&#x27;</span>, <span class="string">&#x27;james&#x27;</span>, <span class="string">&#x27;harden&#x27;</span>],</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]() &#123;</span><br><span class="line">    <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">next</span>: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (index &lt; <span class="built_in">this</span>.names.length)</span><br><span class="line">          <span class="keyword">return</span> &#123; <span class="attr">done</span>: <span class="literal">false</span>, <span class="attr">value</span>: <span class="built_in">this</span>.names[index++] &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">return</span> &#123; <span class="attr">done</span>: <span class="literal">true</span>, <span class="attr">value</span>: <span class="literal">undefined</span> &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="keyword">return</span>() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;被中断了&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">done</span>: <span class="literal">true</span> &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">of</span> iterableObj) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(item)</span><br><span class="line">  <span class="keyword">if</span> (item === <span class="string">&#x27;james&#x27;</span>) <span class="keyword">break</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="什么是生成器"><a href="#什么是生成器" class="headerlink" title="什么是生成器"></a>什么是生成器</h2><p><strong>生成器</strong> 是ES6中新增的一种函数控制、使用的方案，它可以让我们更加灵活的控制函数什么时候继续执行、暂停执</p>
<p>行等操作。</p>
<p>生成器也是一个函数，但是和普通函数存在一些区别：</p>
<ol>
<li>生成器函数在声明的时候需要在 <code>function</code> 后面加一个符号：*。</li>
<li>生成器函数可以通过 <code>yield</code> 关键字来控制函数的执行流程。</li>
<li>生成器函数的返回值是一个生成器。</li>
</ol>
<blockquote>
<p>注意：生成器本质上是一种特殊的迭代器</p>
</blockquote>
<h2 id="生成器函数的执行过程与传参"><a href="#生成器函数的执行过程与传参" class="headerlink" title="生成器函数的执行过程与传参"></a>生成器函数的执行过程与传参</h2><p>通过调用生成器函数，返回一个生成器对象， 然后可以调用生成器对象的 <code>next</code>  方法，执行生成器函数的代码。</p>
<p>在这过程中，可以通过 <code>yield</code> 来返回结果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">foo</span>(<span class="params">m</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;开始执行&#x27;</span>)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 参数m是创建生成器对象的时候传入的参数</span></span><br><span class="line">  <span class="keyword">const</span> value1 = <span class="number">100</span> * m</span><br><span class="line">  <span class="built_in">console</span>.log(value1)</span><br><span class="line">  <span class="keyword">const</span> n = <span class="keyword">yield</span> value1</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 参数n是第二次调用next函数时传入的参数</span></span><br><span class="line">  <span class="keyword">const</span> value2 = <span class="number">200</span> * n</span><br><span class="line">  <span class="built_in">console</span>.log(value2)</span><br><span class="line">  <span class="keyword">yield</span> value2</span><br><span class="line">  </span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;结束执行&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> generator = foo(<span class="number">10</span>)</span><br><span class="line"><span class="built_in">console</span>.log(generator.next())</span><br><span class="line"><span class="built_in">console</span>.log(generator.next(<span class="number">2</span>))</span><br><span class="line"><span class="built_in">console</span>.log(generator.next())</span><br></pre></td></tr></table></figure>

<h2 id="生成器提前结束"><a href="#生成器提前结束" class="headerlink" title="生成器提前结束"></a>生成器提前结束</h2><p>生成器可以调用 <code>return</code> 函数，之后调用 <code>next</code> 函数就不会继续生成值了，返回的生成器的 <code>value</code> 值始终是 <code>undefined</code> 。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">foo</span>(<span class="params">m</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;开始执行&#x27;</span>)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> value1 = <span class="number">100</span> * m</span><br><span class="line">  <span class="built_in">console</span>.log(value1)</span><br><span class="line">  <span class="keyword">const</span> n = <span class="keyword">yield</span> value1</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> value2 = <span class="number">200</span> * n</span><br><span class="line">  <span class="built_in">console</span>.log(value2)</span><br><span class="line">  <span class="keyword">yield</span> value2</span><br><span class="line">  </span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;结束执行&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> generator = foo(<span class="number">10</span>)</span><br><span class="line"><span class="built_in">console</span>.log(generator.next())</span><br><span class="line"><span class="built_in">console</span>.log(generator.return(<span class="number">10</span>))</span><br><span class="line"><span class="built_in">console</span>.log(generator.next())</span><br><span class="line"><span class="built_in">console</span>.log(generator.next())</span><br></pre></td></tr></table></figure>

<h2 id="生成器抛出异常"><a href="#生成器抛出异常" class="headerlink" title="生成器抛出异常"></a>生成器抛出异常</h2><p>可以通过生成器的 <code>throw</code> 函数给生成器函数内部抛出异常。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;开始执行&quot;</span>)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> value1 = <span class="number">100</span></span><br><span class="line">  <span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="keyword">yield</span> value1</span><br><span class="line">  &#125; <span class="keyword">catch</span> (error) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;捕获到异常情况:&quot;</span>, error)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">yield</span> <span class="string">&quot;abc&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> value2 = <span class="number">200</span></span><br><span class="line">  <span class="keyword">yield</span> value2</span><br><span class="line"></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;执行结束&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> generator = foo()</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result = generator.next()</span><br><span class="line">generator.throw(<span class="string">&quot;error&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="使用生成器代替迭代器"><a href="#使用生成器代替迭代器" class="headerlink" title="使用生成器代替迭代器"></a>使用生成器代替迭代器</h2><p>我们之前提到过，生成器是一种特殊的迭代器，因此在一些情况下，我们可以使用生成器来代替迭代器。</p>
<p>有以下三种写法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 生成器来替代迭代器</span></span><br><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">createArrayIterator</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 3.第三种写法 yield* 这种写法是 for of 的语法糖形式</span></span><br><span class="line">  <span class="comment">// yield* arr</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 2.第二种写法</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">of</span> arr) &#123;</span><br><span class="line">    <span class="keyword">yield</span> item</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 1.第一种写法</span></span><br><span class="line">  <span class="comment">// yield &quot;abc&quot; // &#123; done: false, value: &quot;abc&quot; &#125;</span></span><br><span class="line">  <span class="comment">// yield &quot;cba&quot; // &#123; done: false, value: &quot;abc&quot; &#125;</span></span><br><span class="line">  <span class="comment">// yield &quot;nba&quot; // &#123; done: false, value: &quot;abc&quot; &#125;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> names = [<span class="string">&quot;abc&quot;</span>, <span class="string">&quot;cba&quot;</span>, <span class="string">&quot;nba&quot;</span>]</span><br><span class="line"><span class="keyword">const</span> namesIterator = createArrayIterator(names)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br><span class="line"><span class="built_in">console</span>.log(namesIterator.next())</span><br></pre></td></tr></table></figure>

<h2 id="使用生成器实现自定义类的迭代"><a href="#使用生成器实现自定义类的迭代" class="headerlink" title="使用生成器实现自定义类的迭代"></a>使用生成器实现自定义类的迭代</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Team</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name, players</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name</span><br><span class="line">    <span class="built_in">this</span>.players = players</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  *[<span class="built_in">Symbol</span>.iterator]() &#123;</span><br><span class="line">    <span class="keyword">yield</span>* <span class="built_in">this</span>.players</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> team1 = <span class="keyword">new</span> Team(<span class="string">&#x27;gsw&#x27;</span>, [<span class="string">&#x27;curry&#x27;</span>, <span class="string">&#x27;green&#x27;</span>, <span class="string">&#x27;klay&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> team2 = <span class="keyword">new</span> Team(<span class="string">&#x27;lakers&#x27;</span>, [<span class="string">&#x27;ad&#x27;</span>, <span class="string">&#x27;james&#x27;</span>, <span class="string">&#x27;kobe&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> player <span class="keyword">of</span> team1) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(player)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> player <span class="keyword">of</span> team2) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(player)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/search/">搜索</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">1.</span> <span class="toc-text">什么是迭代器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">2.</span> <span class="toc-text">编写一个迭代器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.</span> <span class="toc-text">可迭代对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E7%9A%84%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.</span> <span class="toc-text">内置的可迭代对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">5.</span> <span class="toc-text">可迭代对象的应用场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E7%9A%84%E8%BF%AD%E4%BB%A3"><span class="toc-number">6.</span> <span class="toc-text">自定义类的迭代</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%AD%E4%BB%A3%E5%99%A8%E7%9A%84%E4%B8%AD%E6%96%AD"><span class="toc-number">7.</span> <span class="toc-text">迭代器的中断</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E7%94%9F%E6%88%90%E5%99%A8"><span class="toc-number">8.</span> <span class="toc-text">什么是生成器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E5%87%BD%E6%95%B0%E7%9A%84%E6%89%A7%E8%A1%8C%E8%BF%87%E7%A8%8B%E4%B8%8E%E4%BC%A0%E5%8F%82"><span class="toc-number">9.</span> <span class="toc-text">生成器函数的执行过程与传参</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E6%8F%90%E5%89%8D%E7%BB%93%E6%9D%9F"><span class="toc-number">10.</span> <span class="toc-text">生成器提前结束</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8"><span class="toc-number">11.</span> <span class="toc-text">生成器抛出异常</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%94%9F%E6%88%90%E5%99%A8%E4%BB%A3%E6%9B%BF%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">12.</span> <span class="toc-text">使用生成器代替迭代器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%94%9F%E6%88%90%E5%99%A8%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E7%9A%84%E8%BF%AD%E4%BB%A3"><span class="toc-number">13.</span> <span class="toc-text">使用生成器实现自定义类的迭代</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&text=迭代器与生成器"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&is_video=false&description=迭代器与生成器"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=迭代器与生成器&body=Check out this article: https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&title=迭代器与生成器"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&name=迭代器与生成器&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2022/03/05/JavaScript/iterator-generator/&t=迭代器与生成器"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2021-2022
    柯里的语法糖
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->
 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script> 




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script> 
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

</body>
</html>
